<?php
use \Workerman\Autoloader;

require_once __DIR__ . '/../../../../../Workerman/Autoloader.php';
require_once __DIR__.'/../../../Config/Const.php';
Autoloader::setRootPath(__DIR__.'/../../..');

$page_groupId = GROUP_STATISTICS_MNG;
$page_pageId = PAG_STATISTICS_VISIT_COUNT;

require "../framepages/moudle_base.php";


$needle = $_GET['needle'];
$province = $_GET['province'];
$city = $_GET['city'];
$county = $_GET['county'];
$date = $_GET['date'];

?>
<div class="main-content">
    <div>
        <span><h3>筛选</h3></span>
    </div>
    <div class="search-well">
        <form class="form-inline" style="margin-top: 0px;">
            <table>
                <tr>
                    <!--                    <td width="6%">-->
                    <!--                        模糊搜索-->
                    <!--                    </td>-->
                    <!--                    <td width="15%"> &nbsp;<input class="input-xlarge form-control" size="10"-->
                    <!--                                                  placeholder="品种名称" id="4sStoreName" type="text">-->
                    <!--                    </td>-->

                    <td width="6%">
                        <h5 class="title">地区筛选:</h5>
                    </td>
                    <td width="35%" class="info">
                        <select name="province" id="province">
                            <?php
                            if ($province) {
                                echo"
                                    <option value=''>$province</option>
                                ";
                            } else {
                                echo"
                                    <option value=''>省份</option>
                                ";
                            }
                            ?>
                        </select>
                        <select name="city" id="city">
                            <?php
                            if ($city) {
                                echo"
                                    <option value=''>$city</option>
                                ";
                            } else {
                                echo"
                                    <option value=''>地级市</option>
                                ";
                            }
                            ?>
                        </select>
                        <select name="county" id="county">
                            <?php
                            if ($county) {
                                echo"
                                    <option value=''>$county</option>
                                ";
                            } else {
                                echo"
                                    <option value=''>市、县级市</option>
                                ";
                            }
                            ?>
                        </select>
                    </td>

                    <td width="10%">
                        日期(年-月)：
                    </td>
                    <td>
                        <?php
                        if ($date) {
                            echo "
                                <input type='text' size='10' name='date' id='date' value='$date'>
                            ";

                        } else {
                            $date = date('Y-m');
                            echo "
                                <input type='text' size='10' name='date' id='date' value='$date'>
                            ";
                        }

                        ?>
                    </td>
                    <td>
                        <button class="btn btn-danger" type="button" id="searchButton">
                            <i class="fa fa-search"></i> 查询
                        </button>
                    </td>
                </tr>
            </table>
    </div>
    </form>
</div>
<div>
    <div>
        <span><h2>排名列表</h2></span>
        <table class="table" style="word-break:break-all" style="width: 100%">
            <tr>
                <th width="4%">序号</th>
                <th width="4%">品种名称</th>
                <th width="4%">规格名称</th>
                <th width="4%">规格参数</th>
                <th width="2%">单位</th>
                <th width="4%">省</th>
                <th width="4%">市</th>
                <th width="4%">区/县</th>
                <th width="4%">价格</th>
                <th width="4%">询价次数</th>
                <th width="4%">询价记录</th>
            </tr>

            <?php
            $page_index = $_GET['page_index'];
            if (!$page_index) {
                $page_index = 1;
            }
            $every_page_counts = 30;
            $list_start = ($page_index-1)*$every_page_counts;

            if ($date == date('Y-m') || !$date) {
                $productor_list_array = \Data\Tree_productor_DAO::getListByVisitCount($needle,$province,$city,$county,$list_start,$every_page_counts);

            } else {
                $productor_list_array = \Data\Tree_productor_hist_DAO::getListByVisitCount($needle,$province,$city,$county,$date,$list_start,$every_page_counts);
            }

            $productor_list = $productor_list_array[1];
            $total_count = $productor_list_array[0];
            $page_count = ceil($total_count/$every_page_counts);
            $page_last = $page_index-1;
            $page_next = $page_index+1;
            $i = $list_start+1;

            foreach ($productor_list as $productor_record) {
                $productor_id = $productor_record->getPRODUCTOR_ID();
                $species_id = $productor_record->getSPECIES_ID();
                $species_record = \Data\Tree_species_DAO::getRecord($species_id);
                $species_name = $species_record->getSPECIES_NAME();
                $dw = $species_record->getDW();

                $gg_id = $productor_record->getGG_ID();
                $gg_record = \Data\Tree_gg_DAO::getRecord($gg_id);
                $gg_name = $gg_record->getGG_NAME();
                $gg_dw = $gg_record->getGG_DW();
                $gg_full_name = $gg_name."($gg_dw)";

                $gg_cs = $productor_record->getGG_CS();
                $province = $productor_record->getPROVINCE();
                $city = $productor_record->getCITY();
                $county = $productor_record->getCOUNTY();
                $price = $productor_record->getPRICE();
                $visit_count = $productor_record->getVISIT_COUNT();
                echo "
                        <tr>
                            <td>$i</td>
                            <td>$species_name</td>
                            <td>$gg_full_name</td>
                            <td>$gg_cs</td>
                            <td>$dw</td>
                            <td>$province</td>
                            <td>$city</td>
                            <td>$county</td>
                            <td>$price</td>
                            <td>$visit_count</td>
                            <td><button class='btn btn-danger' onclick='visit_log($productor_id)'>询价记录</button></td>
                        </tr>
                    ";
                $i++;
            }
            ?>

        </table>
    </div>
    <div style="margin-left:35%; margin-top:40px;">
        <div>
            <?php
            if ($page_last) {
                echo "            
                    <button onclick='last_page($page_last)'>上一页</button>                            
                ";
            }

            if ($page_next<=$page_count) {
                echo "         
                    <button onclick='next_page($page_next)'>下一页</button>               
                ";
            }

            if ($page_count>1) {
                echo "
                    &nbsp;&nbsp;&nbsp;&nbsp;去往第<input type='text' name='go_page' id='go_page' size='3'>页
                <button onclick='go_page()'>GO</button>
                    ";
            }
            ?>

        </div>

        <div style="margin-left:5%; margin-top:20px;">
            第<?php echo $page_index ?>页 &nbsp;&nbsp;&nbsp;&nbsp;
            共<?php echo $page_count ?>页  &nbsp;&nbsp;&nbsp;&nbsp;共<?php echo $total_count ?>条记录
        </div>


    </div>
</div>


<?php
require "../framepages/footer.php";
?>
</div>
</div>
<script src="../lib/bootstrap/js/bootstrap.js"></script>
<script src="../lib/location.js"></script>
<script>
    function init() {
        var pro=$("#province");

        for(var i=0;i<province.length;i++){
            var $temp=$("<option value=\"\">"+province[i].name+"</option>");
            pro.append($temp);
        }
    }

    $('#searchButton').click(function() {
            needle = $('#4sStoreName').val();

            province = $("#province option:selected").text();  //获取选中的项
            city = $("#city option:selected").text();  //获取选中的项
            county = $("#county option:selected").text();  //获取选中的项

            date = document.getElementById('date').value;

            if (province.length==0 || province == '省份') {
                province="";
            }
            if (city.length==0 || city == '地级市') {
                city="";
            }
            if (county.length==0 || county == '市、县级市') {
                county="";
            }
            if (date.length==0) {
                alert ('请输入日期');
            }

            url = "?&needle="+needle+"&province="+province+"&city="+city+"&county="+county+
                "&date="+date;
            window.location.href = url;



        }
    );


    function last_page(page_last) {
        needle = $('#4sStoreName').val();

        province = $("#province option:selected").text();  //获取选中的项
        city = $("#city option:selected").text();  //获取选中的项
        county = $("#county option:selected").text();  //获取选中的项

        date = document.getElementById('date').value;

        if (province.length==0 || province == '省份') {
            province="";
        }
        if (city.length==0 || city == '地级市') {
            city="";
        }
        if (county.length==0 || county == '市、县级市') {
            county="";
        }
        if (date.length==0) {
            alert ('请输入日期');
        }

        url = "?page_index="+page_last+"&needle"+needle+"&province="+province+"&city="+city+"&county="+county+
            "&date="+date;
        window.location.href = url;
    }

    function next_page(page_next) {
        needle = $('#4sStoreName').val();
        province = $("#province option:selected").text();  //获取选中的项
        city = $("#city option:selected").text();  //获取选中的项
        county = $("#county option:selected").text();  //获取选中的项

        date = document.getElementById('date').value;

        if (province.length==0 || province == '省份') {
            province="";
        }
        if (city.length==0 || city == '地级市') {
            city="";
        }
        if (county.length==0 || county == '市、县级市') {
            county="";
        }
        if (date.length==0) {
            alert ('请输入日期');
        }

        url = "?page_index="+page_next+"&needle"+needle+"&province="+province+"&city="+city+"&county="+county+
            "&date="+date;
        window.location.href = url;
    }

    function go_page()
    {
        needle = $('#4sStoreName').val();
        province = $("#province option:selected").text();  //获取选中的项
        city = $("#city option:selected").text();  //获取选中的项
        county = $("#county option:selected").text();  //获取选中的项

        date = document.getElementById('date').value;

        if (province.length==0 || province == '省份') {
            province="";
        }
        if (city.length==0 || city == '地级市') {
            city="";
        }
        if (county.length==0 || county == '市、县级市') {
            county="";
        }
        if (date.length==0) {
            alert ('请输入日期');
        }

        url = "?page_index="+$('#go_page').val()+"&needle"+needle+"&province="+province+"&city="+city+"&county="+county+
            "&date="+date;
        window.location.href = url;
    }


    function visit_log(productor_id) {
        date = document.getElementById('date').value;
        url = "visit_log.php?productor_id="+productor_id+"&date="+date;
        window.location.href = url;
    }

    $(function(){
        var pro=$("#province");
        var city=$("#city");
        var dAC=$("#county");
        var num1=null;
        var num2=null;
        //init();

        pro.change(function(){
            num1=pro.prop("selectedIndex");
            city.find("option").eq(0).siblings().remove();
            dAC.find("option").eq(0).siblings().remove();
            if(num1!==0){
                for(var i=0;i<province[num1-1].city.length;i++){
                    var $temp=$("<option value=\"\">"+province[num1-1].city[i].name+"</option>");
                    city.append($temp);
                }
            }
        });

        city.change(function(){
            num2=city.prop("selectedIndex");
            dAC.find("option").eq(0).siblings().remove();
            if(num2!==0){
                for(var i=0;i<province[num1-1].city[num2-1].districtAndCounty.length;i++){
                    var $temp=$("<option value=\"\">"+province[num1-1].city[num2-1].districtAndCounty[i]+"</option>");
                    dAC.append($temp);
                }
            }
        })
    });
</script>
</body>
</html>
